package service;

import objs.*;
import sun.awt.image.ToolkitImage;
import util.PlayMusics;
import util.Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;

public class Play extends JFrame{
    //音乐伴随着对象存在与消失
    Thread thread=new PlayMusics();
    public Play() throws HeadlessException {
        thread.start();
    }
    public static int appearTime=(int)Math.pow(5,5);
    private int w=4096;
    private int h=2303;
    private int count=0;
    public static int state=0;
    public static int score=0;
    public static int bossNum=0;
    private Image offScreenImage=null;//双缓存
    //背景图片
    private Image bg=Toolkit.getDefaultToolkit().getImage("img/pg.jpg");
    //自身
    public static Image me1=Toolkit.getDefaultToolkit().getImage("images/me1.png");
    public static Image me2=Toolkit.getDefaultToolkit().getImage("images/me2.png");
    //自身坠毁
//    诺改变图片尺寸需要创建类通过该类的对象调用drawImage()方法正常显示
//    public static Image img_Me_Destory1=Toolkit.getDefaultToolkit().getImage("img/explode1.png");
    public static Image explode2=Toolkit.getDefaultToolkit().getImage("img/explode2.png");
    public static Image explode3=Toolkit.getDefaultToolkit().getImage("img/explode3.png");
//    public static Image img_Me_Destorya1=Toolkit.getDefaultToolkit().getImage("img/a1.png");
//    public static Image img_Me_Destorya2=Toolkit.getDefaultToolkit().getImage("img/a2.png");
    public static Image img_Me_Destory1=Toolkit.getDefaultToolkit().getImage("images/me_destroy_1.png");
    public static Image img_Me_Destory2=Toolkit.getDefaultToolkit().getImage("images/me_destroy_2.png");
    public static Image img_Me_Destory3=Toolkit.getDefaultToolkit().getImage("images/me_destroy_3.png");
    public static Image img_Me_Destory4=Toolkit.getDefaultToolkit().getImage("images/me_destroy_4.png");
    //敌机
    public static Image img_Enemy= Toolkit.getDefaultToolkit().getImage("images/enemy1.png");
    //敌机坠毁
    public static Image img_Enemy_Down1=Toolkit.getDefaultToolkit().getImage("images/enemy2_down1.png");
    public static Image img_Enemy_Down2=Toolkit.getDefaultToolkit().getImage("images/enemy2_down2.png");
    public static Image img_Enemy_Down3=Toolkit.getDefaultToolkit().getImage("images/enemy2_down3.png");
    public static Image img_Enemy_Down4=Toolkit.getDefaultToolkit().getImage("images/enemy2_down4.png");
    //Boss
    public static Image img_Boss1=Toolkit.getDefaultToolkit().getImage("images/enemy3_n1.png");
    public static Image img_Boss2=Toolkit.getDefaultToolkit().getImage("images/enemy3_n2.png");
    public static Image img_Boss_Hit=Toolkit.getDefaultToolkit().getImage("images/enemy3_hit.png");
    //Boss坠毁
    public static Image img_Boss_Down1=Toolkit.getDefaultToolkit().getImage("images/enemy3_down1.png");
    public static Image img_Boss_Down2=Toolkit.getDefaultToolkit().getImage("images/enemy3_down2.png");
    public static Image img_Boss_Down3=Toolkit.getDefaultToolkit().getImage("images/enemy3_down3.png");
    public static Image img_Boss_Down4=Toolkit.getDefaultToolkit().getImage("images/enemy3_down4.png");
    public static Image img_Boss_Down5=Toolkit.getDefaultToolkit().getImage("images/enemy3_down5.png");
    public static Image img_Boss_Down6=Toolkit.getDefaultToolkit().getImage("images/enemy3_down6.png");
    //自身发射子弹
    private Image img_Bullet= Toolkit.getDefaultToolkit().getImage("images/bullet1.png");
    //Boss发射子弹
    public static Image img_En_Bullet=Toolkit.getDefaultToolkit().getImage("images/bullet2.png");
    //Bomb
    public static Image img_Bomb_Supply=Toolkit.getDefaultToolkit().getImage("images/bomb_supply.png");
    public static Image img_Bomb=Toolkit.getDefaultToolkit().getImage("images/bomb.png");

    //自身发射子弹集合
    public static List<Bulletobj> bulletobjList=new ArrayList<>();
    //Boss发射子弹集合
    public static List<EnBulletobj> enBulletobjList=new ArrayList<>();
    //Boss集合
    public static List<Bossobj> bossobjList=new ArrayList<>();
    //敌机集合
    public static List<Enemyobj> enemyobjList=new ArrayList<>();
    //Boom供应集合
    public static List<Bombobj> bombobjList=new ArrayList<>();
    //Bomb发射集合
    public static List<BombFaseobj> bombFaseobjList=new ArrayList<>();
    //需要删除的物体集合
    public static List<Pojo> removeList=new ArrayList<>();
    //所有物体集合
    public static List<Pojo> pojoList = new ArrayList<>();

    public Bgobj bgobj=new Bgobj(bg,0,-1500,5);
    public Meobj meobj=new Meobj(me1,200,380,102,126,0,this);
//    public Bossobj bossobj=new Bossobj(img_Boss1,0,0,169,258,5,null);

    public void lunch()  {
        setTitle("KingOfHonor");
        setSize(w,h);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(3);
        setResizable(false);
        setVisible(true);//之后执行paint()方法

        pojoList.add(bgobj);
        pojoList.add(meobj);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()==1&&state==0){
                    state=1;
                    repaint();
                }
            }
        });
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==32){
                    switch (state){
                        case 1:
                            state=2;
                            break;
                        case 2:
                            state=1;
                            break;
                        default:
                    }
                }
            }
        });

        while(true){
            if(state==1){
                createobj();
                repaint();
            }
            if(state==0){
                repaint();
            }

            try {
                Thread.sleep(25);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

//    在setVisiable()方法后自动调用
    public void paint(Graphics g) {
        //双缓存
        if(offScreenImage==null){
            offScreenImage=createImage(w,h);
        }
        Graphics graphics=offScreenImage.getGraphics();
        graphics.fillRect(0,0,w,h);

        if(state==0){
            graphics.drawImage(bg, 0, 0, null);
            graphics.drawImage(me1,100,400,null);
            Utils.drawWord(graphics,"点击开始游戏",Color.red,40,180,300);
        }

        if(state==1){


            pojoList.removeAll(removeList);
            for(int i=0;i<pojoList.size();i++){
                pojoList.get(i).paintSelf(graphics);
            }

//            System.out.println(pojoList.size());
        }

        //不能使用else if
        if(state==3){
            while(Meobj.meState!=7){
                switch(Meobj.meState){
                    case 0:
                    case 1:
                        graphics.drawImage(img_Me_Destory1, meobj.getX(),meobj.getY(),null);
                        Meobj.meState=2;
                        break;
                    case 2:
                        graphics.drawImage(img_Me_Destory2, meobj.getX(),meobj.getY(),null);
                        Meobj.meState=3;
                        break;
                    case 3:
                    case 4:
                        graphics.drawImage(img_Me_Destory3, meobj.getX(),meobj.getY(),null);
                        Meobj.meState++;
                        break;
                    default:
                        graphics.drawImage(img_Me_Destory4, meobj.getX(),meobj.getY(),null);
                        Meobj.meState++;
                }
                try {
                    Thread.sleep(25);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Utils.drawWord(graphics,"游戏结束！",Color.green,40,200,400);
        }


        Utils.drawWord(graphics,"得分:"+score,Color.red,80,30,100);
        g.drawImage(offScreenImage,0,0,null);
        count++;
    }

    public void createobj(){
        System.out.println(enemyobjList.size());
        //自身子弹
        if(count%5==0) {
            bulletobjList.add(new Bulletobj(img_Bullet, meobj.getX() + 51, meobj.getY() - 20, 5, 11, 10, this));
            pojoList.add(bulletobjList.get(bulletobjList.size() - 1));
        }
        //敌机
        if(count%16==0) {
//            Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值
            enemyobjList.add(new Enemyobj(img_Enemy,(int)(Math.random()*1000),20,57,43,5,this));
            pojoList.add(enemyobjList.get(enemyobjList.size()-1));
        }
        //Boss子弹
        if(count%5==0&&bossNum!=0) {
//            enBulletobjList.add(new EnBulletobj(img_En_Bullet, bossobj.getX() + 80, bossobj.getY() + 200, 10, 22, 10, this));
//            pojoList.add(enBulletobjList.get(enBulletobjList.size() - 1));
            for(int i=0;i<bossobjList.size();i++) {
                enBulletobjList.add(new EnBulletobj(img_En_Bullet, bossobjList.get(i).getX() + 85, bossobjList.get(i).getY() + 220, 5, 11, 10, this));
                pojoList.add(enBulletobjList.get(enBulletobjList.size() - 1));
            }
        }
        //Boss
        if(count%appearTime==0&&bossNum<5) {
            bossobjList.add(new Bossobj(img_Boss1, (int) (Math.random() * 1000), 0, 169, 258, 5, this));
            pojoList.add(bossobjList.get(bossobjList.size() - 1));
            if (appearTime != 1)
                appearTime /= 5;
            bossNum++;
        }

        if(count%16==0){
            bombobjList.add(new Bombobj(img_Bomb_Supply,(int)(Math.random()*1000),0,60,107,5,this));
            pojoList.add(bombobjList.get(bombobjList.size()-1));
        }
        if(Meobj.launchState==1){
            pojoList.add(new BombFaseobj(img_Bomb,meobj.getX()+20,meobj.getY(),63,57,10,this));
            Meobj.launchState=0;
        }

    }

}

